﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace EightOData.Extensions.WebServices
{
    public static class EbayHelper
    {
        public const String Endpoint = "http://svcs.ebay.com/services/search/FindingService/v1";
        public const String AppId = "Septieme-40f5-4446-8aad-20d94d3d1392";
        public static XNamespace Namespace = "http://www.ebay.com/marketplace/search/v1/services";
        private static readonly HttpClient client = new HttpClient();

        private static String GetUrl(String operation)
        {
            var builder = new StringBuilder();

            builder.Append(Endpoint + "?");
            builder.Append("OPERATION-NAME=" + operation);
            builder.Append("&SECURITY-APPNAME=" + AppId);

            return builder.ToString();
        }

        public static async Task<XDocument> FindItemsByKeywords(string keywords)
        {
            var parameters = "&keywords=" + keywords;
            var url = GetUrl("findItemsByKeywords") + parameters;
            var feed = await client.GetAsync(url);
            var stream = await feed.Content.ReadAsStreamAsync();
            return XDocument.Load(stream);
        }

        public static void ParseXML()
        {
        }
    }
}
